﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WxPayAPI;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xModel;
using Yule8xModel.PageModels;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Test
{
    public partial class AgentRegist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string user_id = Request.QueryString["user_id"];
                tb_user_id.Text = user_id;
                if (user_id.Length > 0)
                {
                    string error;
                    tb_parent1_uid.Text = DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue(string.Format("select parent1_uid from t_user where user_id="+user_id), "0").ToString();
                }
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            int parent1_uid = StringUtil.ToInteger(tb_parent1_uid.Text.Trim());
            string mobile = tb_mobile.Text.Trim();
            string realname = tb_realname.Text.Trim();
            int user_id = StringUtil.ToInteger(tb_user_id.Text.Trim());
            if (string.IsNullOrEmpty(mobile) || user_id < 1 || parent1_uid < 1)
            {
                ResponseUtil.WriteEnd("表单数据不完整");
            }
            if (parent1_uid < 1)
            {
                RedirectUtil.RedirectTimer(false, "出错啦", new string[] { "推荐人ID必需填写!" });
            }
            if (parent1_uid == user_id)
            {
                RedirectUtil.RedirectTimer(false, "出错啦", new string[] { "推荐人ID不能是自己!" });
            }
            if (!UserModels.CheckUserIsAgent(parent1_uid))
            {
                RedirectUtil.RedirectTimer(false, "出错啦", new string[] { "推荐人ID必须已经成为代理!" });
            }

            string attach = string.Format("agentregist:{0},{1}", user_id, "unionid");
            string out_trade_no = WxPayApi.GenerateOutTradeNo();
            decimal agent_regist_amount = AppConf.AgentRegistCardAmount; // 注册成为代理付款金额
            string outmsg;
            bool result = UserModels.UserRegistAgentSubmit(user_id, agent_regist_amount, out_trade_no, mobile, realname, parent1_uid, attach, out outmsg);
            if (result)
            {
                // 微信支付
                string sqlstmt = string.Format("select openid_biz from t_user where user_id=" + user_id);
                string openid = DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue(sqlstmt, "0").ToString();
                int number = AppConf.AgentRegistCardCount; // 注册成为代理购买钻石数量
                decimal amount = AppConf.AgentRegistCardAmount; // 注册成为代理付款金额

                int rebate_amount = AppConf.AgentRegistReturnAmount;
                decimal parent1_amount = amount * AppConf.Parent1AgentCommissionRate; // 上级是代理, 获得25%返佣
                decimal parent2_amount = amount * AppConf.Parent2AgentCommissionRate; // 上上级是代理, 获得15%返佣
                string paramstr = string.Empty;
                result = UserModels.PayForAgentRegist(user_id, amount, number, rebate_amount, parent1_amount, parent2_amount, AppInfo.EncryptKey, out_trade_no, out outmsg);
                Response.Write(string.Format("result={0}, message={1}", result?"成功":"失败", outmsg));
            }
            else
            {
                Response.Write("出错啦 " + outmsg);
            }

        }
    }
}